import { observable, action, toJS } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';

const { TableStore } = WeaTableNew;
import { WeaTools } from 'ecCom';
import { message } from 'antd';
import * as Apis from '../apis/cardManage';
import { CrmContactStore } from './crmContactStore';
import { crmAddFormRules } from '../util/index';
import { CrmMessageStore } from './crmMessageStore';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
export class CardManageStore {
    @observable title = '查询客户';
    leftParams={};
    @observable selectTabKey = '1';
    @observable showSearchAd = false;
    @observable form = new WeaForm();
    @observable manageCondition = [];
    @observable mineCrmContactStore = new CrmContactStore();
    @observable formParams = {};

    @observable contacterCardData = [];
    @observable contacterCardTotal = 0;
    @observable addContacterform = new WeaForm();
    @observable addContacterCondition = [];

    @observable sellChanceCardData = [];
    @observable sellChanceCardDataTotal = 0;
    @observable crmMessageStore = new CrmMessageStore();


    @action
    setParamsFromLeft=(params) => {
      this.leftParams = params;
    }
    doSearch=(params = {}) => {
      const searchParamsAd = this.form.getFormParams();
      const newParams = { ...searchParamsAd, ...params };
      if (this.selectTabKey == '1' || this.selectTabKey == '3') { // 客户联系
        this.formParams = newParams;
      }
      if (this.selectTabKey == '2') { // 客户联系
        this.getContacterInfo(newParams);
      }
      if (this.selectTabKey == '4') { // 销售机会
        this.getSellChanceInfo(newParams);
      }
    }

    setTabKey=(key) => {
      this.selectTabKey = key;
      this.setShowSearchAd(false);
      if (key == '1') { // 客户联系
        this.contactLogCondition();
      }
      if (key == '2') { // 联系人
        this.contacterCardCondition();
        this.getContacterInfo();
      }
      if (key == '3') { // 客户留言
        this.leaveMessageCondition();
      }
      if (key == '4') { // 销售机会
        this.sellChanceCardCondition();
        this.getSellChanceInfo();
      }
    }
    contactLogCondition=(params) => {
      this.form = new WeaForm();
      Apis.contactLogCondition(params).then((data) => {
        if (data.status == 'success') {
          this.manageCondition = data.datas;
          this.form && !this.form.isFormInit && this.form.initFormFields(data.datas);
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    contacterCardCondition=(params) => {
      this.form = new WeaForm();
      Apis.contacterCardCondition(params).then((data) => {
        if (data.status == 'success') {
          this.manageCondition = data.datas;
          this.form && !this.form.isFormInit && this.form.initFormFields(data.datas);
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    leaveMessageCondition=(params) => {
      this.form = new WeaForm();
      Apis.leaveMessageCondition(params).then((data) => {
        if (data.status == 'success') {
          this.manageCondition = data.datas;
          this.form && !this.form.isFormInit && this.form.initFormFields(data.datas);
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    sellChanceCardCondition=(params) => {
      this.form = new WeaForm();
      Apis.sellchanceCardCondition(params).then((data) => {
        if (data.status == 'success') {
          this.manageCondition = data.datas;
          this.form && !this.form.isFormInit && this.form.initFormFields(data.datas);
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    getContacterInfo=(params) => {
      Apis.getContacterCard({ ...this.leftParams, ...params }).then((data) => {
        if (data.status == 'success') {
          this.contacterCardData = data.datas.rsList;
          this.contacterCardTotal = data.datas.totalSize;
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    addContacterForm=(params) => {
      this.addContacterform = new WeaForm();
      Apis.getContacterAddForm(params).then((data) => {
        if (data.status == 'success') {
          this.addContacterCondition = data.datas;
          this.addContacterform && !this.addContacterform.isFormInit && this.addContacterform.initFormFields(crmAddFormRules(data.datas));
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 新建联系人保存
    saveContacter=(params) => {
      Apis.saveContacterAdd(params).then((data) => {
        if (data.status == 'success') {
          const searchParamsAd = this.form.getFormParams();
          const newParams = { customerId: params.customerId, ...searchParamsAd };
          this.getContacterInfo(newParams);
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 销售机会信息获取
    getSellChanceInfo=(params) => {
      Apis.getSellchanceCard({ ...this.leftParams, ...params }).then((data) => {
        if (data.status == 'success') {
          this.sellChanceCardData = data.datas.rsList;
          this.sellChanceCardDataTotal = data.datas.totalSize;
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }

    setShowSearchAd=(bool) => {
      this.showSearchAd = bool;
    }

    appendFormFields=(value) => {
      this.form.updateFields(value, false);
    }

    clearFormFields=() => {
      this.form.resetConditionValue(); // 清除查询条件值
    }
}

